//
//  LoginViewController.m
//  SDSY
//
//  Created by 蓝布鲁 on 2017/3/26.
//  Copyright © 2017年 蓝布鲁. All rights reserved.
//

#import "LoginViewController.h"
#import "UserInfoModel.h"
#import "MainViewController.h"
#import "AppDelegate.h"

@interface LoginViewController ()<UITextFieldDelegate,UIScrollViewDelegate>
// 拖线的属性
@property (weak, nonatomic) IBOutlet UITextField *idTextField;          // 学号id
@property (weak, nonatomic) IBOutlet UITextField *passwdTextField;      // 密码
@property (weak, nonatomic) IBOutlet UIButton *loginBtn;                // 登录按钮
@property (weak, nonatomic) IBOutlet UIView *labelBackView;             // label背景View


@end

@implementation LoginViewController



- (void)viewDidLoad {
    [super viewDidLoad];
    // 检查是否已经登录,如果已经登录,则进入主界面
    if ([[UserInfoModel shareInstance] checkLogin]) {
        // 已登录
        ((AppDelegate *)[UIApplication sharedApplication].delegate).window.rootViewController = [[MainViewController alloc] init];
        return;
    }
    
    // 调整登录按钮
    self.loginBtn.layer.masksToBounds = true;
    self.loginBtn.layer.cornerRadius = 10;
    
    // 调整背景View
    self.labelBackView.layer.masksToBounds = true;
    self.labelBackView.layer.cornerRadius = 10;
    self.labelBackView.layer.borderWidth = 0.5;
    self.labelBackView.layer.borderColor = [UIColor grayColor].CGColor;
    
    // textField
    self.idTextField.delegate = self;
    self.passwdTextField.delegate = self;

    
    
#pragma mark 测试账号自动填充
//    self.idTextField.text = @"201604070102";
//    self.passwdTextField.text = @"111111";
    
}

-(void)dealloc {
    [[NSNotificationCenter defaultCenter]removeObserver:self];
}

#pragma mark - 事件监听
// 登录按钮
- (IBAction)loginBtnTapped:(id)sender {
    [self login];
}

#pragma mark - 网络请求

// 登录
- (void)login {
    [SVProgressHUD show];
    // 初始化参数
    NSDictionary *parameters = @{@"phone": self.idTextField.text, @"pwd": [self.passwdTextField.text MD5String]};
    
    [[NetworkTool shareInstance] requireMethodType:POSTType URLString:@"SDSY/TLogin" parameters:parameters success:^(NSDictionary *respondDictionary) {
        // 如果为空的模型,直接返回
        if ([respondDictionary allKeys].count == 0) {
            [SVProgressHUD dismiss];
            [SVProgressHUD showErrorWithStatus:@"账号或密码不正确"];
            return;
        }
        // 设置用户模型
        [[UserInfoModel shareInstance] setValuesForKeysWithDictionary:respondDictionary];
        [SVProgressHUD dismiss];
        
        // 登录成功,转到主页
        ((AppDelegate *)[UIApplication sharedApplication].delegate).window.rootViewController = [[MainViewController alloc] init];
    } failure:^(NSError *error) {
        NSLog(@"%@",error);
        [SVProgressHUD dismiss];
        [SVProgressHUD showErrorWithStatus:@"账号或密码不正确"];
    }];
}

#pragma mark - 自定义方法


#pragma mark - UITextFieldDelegate协议
-(BOOL)textFieldShouldReturn:(UITextField *)textField {
    if (textField.tag == 1) {
        // 学号textField,点击next,密码textField聚焦
        [self.passwdTextField becomeFirstResponder];
    }else {
        // 密码textField,点击return,直接登录
        [self login];
        [self.idTextField resignFirstResponder];
        [self.passwdTextField resignFirstResponder];
    }
    
    return true;
}

#pragma mark - 系统回调
-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    [self.idTextField resignFirstResponder];
    [self.passwdTextField resignFirstResponder];
}



@end
